﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class SeasonList : Form
    {
        public SeasonList()
        {
            InitializeComponent();
        }

        private void add_Click(object sender, EventArgs e)
        {
            NewSeason ns = new NewSeason();
            ns.ShowDialog();
            UpdateSeasonList();
        }

        private void SeasonList_Load(object sender, EventArgs e)
        {
            UpdateSeasonList();
        }

        private void UpdateSeasonList()
        {
            dgvSeasons.Rows.Clear();

            SqlDataReader dataReader = Global.getDataReader("[getAllSeasons]");
            while (dataReader.Read())
            {
                dgvSeasons.Rows.Add(dataReader["id"], dataReader["name"]);

            }
            dataReader.Close();
            Global.closeConnection();
        }

        private void dgvClubs_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSeasons.SelectedRows.Count > 0)
            {
                tournir.Enabled = true;
                statistic.Enabled = true;
                del.Enabled = true;
            }
            else 
            {
                tournir.Enabled = false;
                statistic.Enabled = false;
                del.Enabled = false;            
            }
        }

        private void tournir_Click(object sender, EventArgs e)
        {
            int seasId = Convert.ToInt32( dgvSeasons.SelectedRows[0].Cells["id"].Value);

            TourInfo ti = new TourInfo(seasId);
            ti.ShowDialog();

            UpdateSeasonList();
        }

        private void del_Click(object sender, EventArgs e)
        {
            int seasId = Convert.ToInt32(dgvSeasons.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delSeason]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = seasId;

            conn.Open();
            myCommand.ExecuteScalar();
            conn.Close();

            UpdateSeasonList();
        }

        private void statistic_Click(object sender, EventArgs e)
        {
            int seasId = Convert.ToInt32(dgvSeasons.SelectedRows[0].Cells["id"].Value);

            SeasonTable st = new SeasonTable(seasId);
            st.ShowDialog();
        }

    }
}
